package com.traffic.data;

import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;

public class LobUtil {

    public static String clobToString(Clob clob) {
        if (clob == null) {
            return null;
        }
        try {
            Reader r = clob.getCharacterStream();
            StringWriter sw = new StringWriter();
            char[] cs = new char[(int) clob.length()];
            try {
                r.read(cs);
                sw.write(cs);
                return sw.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] blobToBytes(Blob blob) {
        if (blob == null) {
            return null;
        }
        try {
            return blob.getBytes(1L, (int) blob.length());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // public static void setClob(DBConn conn, PreparedStatement ps, int i,
    // Object v) throws SQLException {
    // if (conn.getDBType().equals(DBConnConfig.ORACLE)) {
    // OracleConnection oc = (OracleConnection) conn.getPhysicalConnection();
    // CLOB clob = null;
    // try {
    // clob = CLOB.createTemporary(oc, true, 1);//
    // Oracle9i中是1,10G中变成了10，但还是会将1自动转为10
    // clob.open(1);
    // Writer writer = clob.setCharacterStream(0L);
    // writer.write(String.valueOf(v));
    // writer.close();
    // clob.close();
    // ps.setClob(i + 1, clob);
    // } catch (Exception e) {
    // try {
    // if (clob != null) {
    // CLOB.freeTemporary(clob);
    // }
    // } catch (SQLException e1) {
    // e1.printStackTrace();
    // }
    // e.printStackTrace();
    // }
    // } else {
    // ps.setObject(i + 1, v);
    // }
    // }
}
